package com.gccloud.tgyfirst.controller.demo;

import com.gccloud.tgyfirst.demo.dto.StudentCourseDTO;
import com.gccloud.tgyfirst.demo.entity.StudentCourseEntity;
import com.gccloud.tgyfirst.service.demo.service.IStudentCourseService;

import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.controller.SuperController;
import com.gccloud.starter.plugins.validator.ValidatorUtils;
import com.gccloud.starter.plugins.validator.group.Insert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author qianxing
 * @version 1.0
 * @date 2021/5/17 16:50
 */
@RestController
@RequestMapping("/select")
@Api(tags = "选课")
@ApiSort(value = 30)
public class SelectCourseController extends SuperController {

    @Resource
    private IStudentCourseService studentCourseService;

    /**
     * 选择课程
     * @param studentCourseDTO
     * @return
     */
    @PostMapping
    @RequiresPermissions("select:add")
    @ApiOperation(value = "新增", position = 10, notes = "选课", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> select(@ApiParam(name = "新增选课JSON对象", value = "传入json格式", required = true) @RequestBody StudentCourseDTO studentCourseDTO){
        ValidatorUtils.validateEntity(studentCourseDTO, Insert.class);
        StudentCourseEntity entity =  BeanConvertUtils.convert(studentCourseDTO, StudentCourseEntity.class);
        studentCourseService.select(entity);
        return success();
    }


    /**
     * 取消选择课程
     * @param courseId
     * @return
     */
    @DeleteMapping("/{courseId}")
    @RequiresPermissions("select:delete")
    @ApiOperation(value = "删除", notes = "取消选择课程", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteById(@ApiParam(name = "课程ID", value = "传入String格式", required = true) @PathVariable("courseId") String courseId) {
        studentCourseService.cancelSelect(courseId);
        return success();
    }

}
